<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

/**
 * Attempts to efficiently download a file.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 * @param string $file
 * @param string $type Document type (e.g. "html")
 * @param bool $accelerate
 */
function download($file, $type, $accelerate = true) {
  global $mime;
  $mime_type = $mime[$type];
  if (ob_get_level()) ob_end_clean();
  $mtime = filemtime($file);
  $mdate = gmdate($format = 'D, d M Y H:i:s', $mtime) . ' GMT';
  header($_SERVER['unique'] ? 'Expires: ' . gmdate($format, strtotime('+1 year')
    ) . ' GMT' : 'Cache-Control: max-age=0, must-revalidate');
  header("Content-type: $mime_type");
  header("Last-Modified: $mdate");
  header('Content-Length: ' . filesize($file));
  // to install the http extension run the following commands:
  // sudo apt-get install php-http
  // sudo pecl install pecl_http
  // add
  // extension=json.so
  // extension=raphf.so
  // extension=propro.so
  // extension=http.so
  // into php.ini
  if (extension_loaded('http') && function_exists('http_cache_last_modified') &&
      !http_cache_last_modified($mtime)) {
    exit;
  }
  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
      ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $mdate)) {
    header('HTTP/1.1 304 Not Modified');
    exit;
  }
  // on nginx add the following into your virtual host configuration
  // location /cache/ {
  //   internal;
  //   root <web root>; // the absolute path to the "web" directory
  // }
  if ($accelerate &&  preg_match('/nginx/i', $_SERVER['SERVER_SOFTWARE'])) {
    header("X-Accel-Redirect: /$file");
    exit;
  }
  // on apache - install mod_xsendfile and then in the virtual host
  // configuration:
  // <Directory "<web root>"> // the absolute path to the "web" directory
  //   XSendFile On
  //   XSendFilePath <web root>/cache
  // </Directory>
  if ($accelerate && preg_match('/apache/i', $_SERVER['SERVER_SOFTWARE']) &&
      in_array('mod_xsendfile', apache_get_modules(), true)) {
    header('X-Sendfile: ' . realpath($file));
    exit;
  }
  readfile($file);
  exit;
}